package com.dongan.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongan.pojo.Apply;
import com.dongan.pojo.Codes;
import com.dongan.pojo.File;
import com.dongan.service.ApplyService;
import com.dongan.service.CodesService;
import com.dongan.utils.NumberFormatUtil;
import com.dongan.utils.ResultResponseUtil;
import com.dongan.utils.SerialNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品生产计划
 * @author longTao
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/apply")
@CrossOrigin
public class ApplyController {
    @Autowired
    private ApplyService service;

    @Autowired
    private CodesService codesService;
    /**
     * 根据条件分页查询生产计划
     * @return
     */
    @GetMapping("/queryPageApply")
    public Object queryPageApply(String checkTag,String manufactureTag,String pageNum,String pageSize){
        QueryWrapper<Apply> queryWrapper=new QueryWrapper<>();
        Page<Apply> page=new Page<>();
        if(!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("CHECK_TAG",checkTag);
        }
        if(!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("MANUFACTURE_TAG",manufactureTag);
        }
        if(StringUtils.isEmpty(pageNum)){
            pageNum="1";
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize="2";
        }
        page.setCurrent(Integer.valueOf(pageNum));
        page.setSize(Integer.valueOf(pageSize));
        Page<Apply> applies= service.page(page,queryWrapper);
        return new ResultResponseUtil(applies);
    }

    /**
     * 根据条件查询生产计划
     * @return
     */
    @GetMapping("/queryApplyByApplyId")
    public Object queryApplyByApplyId(String applyId){
        QueryWrapper<Apply> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("APPLY_ID",applyId);
        List<Apply> applies= service.list(queryWrapper);
        return new ResultResponseUtil(applies);
    }
    /**
     * 添加生产计划
     * @param applys
     * @return
     */
    @PostMapping("addApply")
    public Object addPaaly(@RequestBody List<Apply> applys){
        //生产计划编号	300+当前日期+4位流水号
        String id=300+ NumberFormatUtil.numberFormat2(4, SerialNumber.applySN);
        boolean flag=true;
        for (Apply apply : applys) {
            apply.setApplyId(id);
            apply.setCheckTag("S001-0");
            apply.setManufactureTag("P001-0");
            flag=service.save(apply);
            if(flag){
                Codes codes=new Codes();
                codes.setId(4L);
                codes.setDefValue(Integer.valueOf(SerialNumber.applySN).toString());
                codesService.updateById(codes);
            }
        }
        //CHECK_TAG	S001-0: 等待审核	审核标志
        //MANUFACTURE_TAG	P001-0: 未派工	派工标志
        return new ResultResponseUtil(flag,"添加完成!");
    }
    @PostMapping("/checkApply")
    public Object checkApply(@RequestBody List<Apply> applies){
        boolean flag=true;
        for (Apply apply : applies) {
            flag=service.updateBatchById(applies);
        }
        return new ResultResponseUtil(flag);
    }
}

